package com.efeichong.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author lxk
 * @date 2020/12/3
 * @description mvc配置 可以访问接口文档doc.html和磁盘中的文件
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${oss.local.viewUrlPrefix: }")
    private String staticPathPattern;

    @Value("${oss.local.basedir: }")
    private String staticLocations;

    /**
     * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!staticLocations.endsWith("/")) {
            staticLocations = staticLocations + "/";
        }
        registry.addResourceHandler(staticPathPattern + "/**")
                .addResourceLocations("file:" + staticLocations);
    }

    /**
     * 默认访问根路径跳转 doc.html页面 （swagger文档页面）
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("doc.html");
    }
}
